package com.ctg.behavior.calc.impala.helper;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;

import com.ctg.behavior.calc.common.CtStepEvent;
import com.ctg.behavior.calc.impala.utils.CtStepUtils;
import com.ctg.behavior.common.consts.CtFieldConsts;

public class CtFunnelCalcHelper {
	
	public static boolean isSameByValues(List<String> valuesListR, List<String> valuesListL) {
		if(valuesListR==null || valuesListL==null) {
			return false;
		}
		if(valuesListR.size() == 1 && valuesListL.size() ==1) {
			String rValue = valuesListR.get(0);
			String lValue = valuesListL.get(0);
			if(rValue!=null && rValue.equals(lValue)) {
				return true;
			}
		}
		return false;
	}

	// KEY: event + "@" + day + "@" + byValuesList
	public static String buildQueryResultKey(LinkedHashMap<String, Object> oneQueryRecord, Integer stepNo) {
		//StringBuilder sbuf = new StringBuilder();
		String event = String.valueOf(oneQueryRecord.get(CtFieldConsts.FIELD_KEY_EVENT));
		String eventDate = String.valueOf(oneQueryRecord.get(CtFieldConsts.FIELD_KEY_EVENT_DATE));
		//sbuf.append(event).append("@").append(eventDate);
		Set<Entry<String, Object>> entries = oneQueryRecord.entrySet();
		List<String> byValues = new ArrayList<>();
		for (Entry<String, Object> entry : entries) {
			String key = entry.getKey();
			if (CtFieldConsts.FIELD_KEY_EVENT.equals(key) || CtFieldConsts.FIELD_KEY_EVENT_DATE.equals(key) || CtFieldConsts.FIELD_KEY_TOTAL.equals(key)) {
				continue;
			}
			byValues.add(String.valueOf(entry.getValue()));
			//byValues.add(entry.getValue().toString());
			//sbuf.append("@").append(entry.getValue());
		}
		if(byValues.isEmpty()) {
			byValues.add("$ALL");
		}
		//return sbuf.toString();
		CtStepEvent stepEvent = CtStepUtils.buildStepEvent(event, stepNo);
		return CtFunnelCalcHelper.buildRowKey(eventDate, byValues, stepEvent);
	}

	// KEY: event + "@" + day + "@" + byValuesList
	public static String buildQueryResultKey(String day, List<String> byValues, CtStepEvent stepEvent) {
		return CtFunnelCalcHelper.buildRowKey(day, byValues, stepEvent);
	}

	// KEY: event + "@" + day + "@" + byValuesList
	public static String buildOverviewRowsKey(String day, List<String> byValues, CtStepEvent stepEvent) {
		return CtFunnelCalcHelper.buildRowKey(day, byValues, stepEvent);
	}

	// KEY: event + "@" + day + "@" + byValuesList
	public static String buildStepsRowsKey(String day, List<String> byValues, CtStepEvent stepEvent) {
		return CtFunnelCalcHelper.buildRowKey(day, byValues, stepEvent);
	}

	private static String buildRowKey(String day, List<String> byValues, CtStepEvent stepEvent) {
		StringBuilder sbuf = new StringBuilder();
		sbuf.append(stepEvent.getStepNo()).append("@");
		sbuf.append(stepEvent.getEventId()).append("@").append(day);
		for (String byValue : byValues) {
			sbuf.append("@").append(byValue);
		}
		return sbuf.toString();
	}

}
